package com.flink.datastreamapi.source;


import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class KafkaSourceDemo {
    public static void main(String[] args) throws Exception {

        //获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // env.setParallelism(1);
        //读取数据源--Kafka
        //从kafka读取数据
        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers("10.90.100.101:9092")//设置kafka节点的地址和端口
                .setGroupId("my-group") //设置消费者组的id
                .setTopics("topic_1")//设置消费的topic
                // 指定消费kafka的策略，这里设置从最新的位置开始消费
                .setStartingOffsets(OffsetsInitializer.latest())
                //反序列化，这里设置对字符串进行反序列化
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        DataStreamSource<String> source = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafka-source");


        //输出
        source.print();

        //执行
        env.execute();


    }
}
